package com.xls.dormitoryservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.base.model.Employee;
import com.xls.dormitoryservice.service.EmployeeService;
import com.xls.dormitoryservice.vo.EmployeeQuery;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/dormitory/employee")
@Api(value="宿舍管理员接口")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Log(title = "员工管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:employee:list')")
    @GetMapping("/getEmployee/{id}")
    public Result<Employee> getEmployee(@PathVariable String id) {
        return Result.success(employeeService.getById(id));
    }

    @Log(title = "员工管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:employee:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<Employee>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                    @RequestBody EmployeeQuery employeeQuery) {
        return Result.success(employeeService.getEmployeePage(page, limit, employeeQuery));
    }

    @Log(title = "员工管理", businessType = "保存")
    @PreAuthorize("hasAnyAuthority('dormitory:employee:save')")
    @PostMapping("/save")
    public Result<Employee> save(@RequestBody Employee employee) {
        return employeeService.saveOrUpdate(employee) ? Result.success(employee) : Result.error();
    }

    @Log(title = "员工管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('dormitory:employee:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<Employee> remove(@PathVariable String id) {
        return employeeService.removeById(id) ? Result.success() : Result.error();
    }

}

